/*
 ============================================================================
 Name        : BlockSetParam.cpp
 Author      : 
 Version     :
 Copyright   : 
 Description : CBlockSetParam implementation
 ============================================================================
 */

#include "BlockSetParam.h"
#include "eStockappui.h"
#include "Global.h"
#include <eStock_L2hy.rsg>
#include "BlockPicHis.h"
CBlockSetParam::CBlockSetParam(CBaseBlock* bsBack) :
	CBlock(bsBack), m_nIndSize(9), m_nIndCurrent(0), m_nCurrent(0)
{
}

CBlockSetParam::~CBlockSetParam()
{
	m_Ctrls.ResetAndDestroy();
}

CBlockSetParam* CBlockSetParam::NewLC(CBaseBlock* bsBack)
{
	CBlockSetParam* self = new (ELeave) CBlockSetParam(bsBack);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockSetParam* CBlockSetParam::NewL(CBaseBlock* bsBack)
{
	CBlockSetParam* self = CBlockSetParam::NewLC(bsBack);
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockSetParam::ConstructL()
{
	CBlock::ConstructL();
	CGlobal::GetDesFromID(m_strOK, LOC_BUF_MENU_SAVE);
	CGlobal::GetDesFromID(m_strTitle, LOC_BUF_STR_SETPARA);

	TInt temparr[9] =
	{
	3, 2, 3, 3, 2, 1, 1, 1, 2
	};
	memcpy(m_pnSize, temparr, sizeof(temparr));
	memcpy(m_pnParam, m_pGlobal->m_pnParam, sizeof(m_pGlobal->m_pnParam));

	m_selBox = CSelectBox::NewL();
	m_selBox->AddItem(0, LOC_BUF_STR_PARAK);
	m_selBox->AddItem(1, LOC_BUF_STR_PARAEXC);
	m_selBox->AddItem(2, LOC_BUF_STR_PARAMACD);
	m_selBox->AddItem(3, LOC_BUF_STR_PARAKDJ);
	m_selBox->AddItem(4, LOC_BUF_STR_PARARSI);
	m_selBox->AddItem(5, LOC_BUF_STR_PARAWR);
	m_selBox->AddItem(6, LOC_BUF_STR_PARAVR);
	m_selBox->AddItem(7, LOC_BUF_MENU_ZIJINLIUBIAN);
	m_selBox->AddItem(8, LOC_BUF_MENU_DADANBILV);

	TBuf<8> buf;
	CGlobal::GetDesFromID(buf, LOC_BUF_STR_PARA); //" 指标参数"
	buf.AppendNum(1);
	buf.Append(_L(": "));

	m_input1 = CSingleLineTextInput::NewL(buf, 3);
	buf[4] = '2';
	m_input2 = CSingleLineTextInput::NewL(buf, 3);
	buf[4] = '3';
	m_input3 = CSingleLineTextInput::NewL(buf, 3);
	m_Ctrls.Append(m_selBox);

	m_Ctrls.Append(m_input1);
	m_Ctrls.Append(m_input2);
	m_Ctrls.Append(m_input3);

	if (CGlobal::Static()->m_osMajor == 3)
		m_Ctrls[0]->SetFocus(ETrue);
}

void CBlockSetParam::BeforeDraw(CFbsBitGc& g) const
{
	iTitlebar->SetTitleText(m_strTitle);
	CBlock::BeforeDraw(g);
}

void CBlockSetParam::SizeChanged()
{
	CBlock::SizeChanged();
	iToolbar->SetSoftKey(CToolbar::EOK, CToolbar::EBack);
	for (int i = 0; i < m_Ctrls.Count(); i++)
	{
		TInt y = (i + 1) * (m_nLineHeight + 4);
		m_Ctrls[i]->SetRect(0, y, m_nScreenWidth, y + m_nLineHeight - 1);
		if (i > 0)
		{
			((CSingleLineTextInput*) m_Ctrls[i])->SetRange(0, 365);
			TBuf<4> buf;
			buf.AppendNum(m_pnParam[m_selBox->GetCurValue() * 3 + i - 1]);
			((CSingleLineTextInput*) m_Ctrls[i])->SetText(buf);
		}
	}
}

void CBlockSetParam::OnDraw(CFbsBitGc& g) const
{
	//g.UseFont(m_pGlobal->g_font);
	for (TInt i = 0; i <= m_pnSize[m_selBox->GetCurValue()]; i++)
		m_Ctrls[i]->Draw(g);
}

void CBlockSetParam::OnKeyEvent(const TInt& keyCode)
{
	if (keyCode == m_pGlobal->g_nKeyOK || keyCode == m_pGlobal->g_nKeyCancel
			|| keyCode == EStdKeyNkpAsterisk || keyCode == '*')
	{
		if (keyCode == m_pGlobal->g_nKeyOK || keyCode == '*' || keyCode
				== EStdKeyNkpAsterisk)
		{
			for (int i = 0; i < 30; i++)
				m_pGlobal->m_pnParam[i] = m_pnParam[i];
		}
		((CBlockPicHis*) m_pBlockBack)->ResetDay();
		//((CBlockPicHis*)m_pBlockBack)->m_bFresh = 0;
		((CBlockPicHis*) m_pBlockBack)->SetIndType(
				((CBlockPicHis*) m_pBlockBack)->m_bIndType);
		if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
			eStockUI->GetCurView()->iContainer->DelBlock();
		return;
	}

	if (keyCode == m_pGlobal->g_nKeyUp || keyCode == m_pGlobal->g_nKeyDown)
	{
		m_Ctrls[m_nCurrent]->SetFocus(EFalse);
		if (keyCode == m_pGlobal->g_nKeyUp)
		{
			m_nCurrent--;
			if (m_nCurrent < 0)
				m_nCurrent = m_pnSize[m_selBox->GetCurValue()];
		}
		else
		{
			m_nCurrent++;
			if (m_nCurrent > m_pnSize[m_selBox->GetCurValue()])
				m_nCurrent = 0;
		}
		m_Ctrls[m_nCurrent]->SetFocus(ETrue);
		DrawNow();
		return;
	}

	if (m_Ctrls[m_nCurrent]->OnKeyDown(keyCode))
	{
		TInt n = m_selBox->GetCurValue();
		if (m_nCurrent == 0)
		{
			for (TInt i = 1; i <= m_pnSize[n]; i++)
			{
				CSingleLineTextInput* input =
						(CSingleLineTextInput*) m_Ctrls[i];
				TBuf<4> temp;
				temp.AppendNum(m_pnParam[n * 3 + i - 1]);
				input->SetText(temp);
			}
		}
		else
		{
			CSingleLineTextInput* input =
					(CSingleLineTextInput*) m_Ctrls[m_nCurrent];
			m_pnParam[n * 3 + m_nCurrent - 1] = input->GetIntValue();
		}
		DrawNow();
	}
}

void CBlockSetParam::OnMouseUp(const TPoint& aPt)
{
	TInt nPos = -1;
	TInt nSize = m_pnSize[m_selBox->GetCurValue()];
	for (TInt i = 0; i <= nSize; i++)
	{
		TInt top = m_Ctrls[i]->m_rect.iTl.iY;
		TInt bottom = m_Ctrls[i]->m_rect.iBr.iY;
		if (aPt.iY > top && aPt.iY < bottom)
		{
			nPos = i;
			break;
		}
	}
	if (nPos >= 0)
	{
		m_Ctrls[m_nCurrent]->SetFocus(EFalse);
		m_nCurrent = nPos;
		m_Ctrls[m_nCurrent]->SetFocus(ETrue);
		if (m_Ctrls[m_nCurrent] == m_selBox)
		{
			if (aPt.iX < m_nLineHeight)
				OnKeyEvent(m_pGlobal->g_nKeyLeft);
			else if (aPt.iX > m_nScreenWidth - m_nLineHeight)
				OnKeyEvent(m_pGlobal->g_nKeyRight);
		}
		else
		{
			m_kb->SetInputType(CKeyBoard::NUMERIC);
		}
		DrawNow();
	}
}

void CBlockSetParam::OnInput(const TDesC8& strInput)
{
	if (m_kb->m_bInputGoodCode)
	{
		CBlock::OnInput(strInput);
		return;
	}
	
	TBuf<32> buf;
	buf.Copy(strInput);
	static_cast<CSingleLineTextInput*>(m_Ctrls[m_nCurrent])->SetText(buf);
	
	TInt n = m_selBox->GetCurValue();
	CSingleLineTextInput* input = (CSingleLineTextInput*) m_Ctrls[m_nCurrent];
	m_pnParam[n * 3 + m_nCurrent - 1] = input->GetIntValue();
	DrawNow();
}
